package com.holystone.splunk.app.service.impl;

import com.holystone.splunk.app.service.ISplunkBaseService;
import com.splunk.SSLSecurityProtocol;
import com.splunk.Service;
import com.splunk.ServiceArgs;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;

/**
 * Copyright (c) 2017, HOLYSTONE Technologies, Inc.
 * All right reserved.
 *
 * @author huyan
 * @date 2017/12/1
 * @desc:
 */
@org.springframework.stereotype.Service(value = "splunkBaseService")
public class SplunkBaseServiceImpl implements ISplunkBaseService {

    private Logger logger = Logger.getLogger(this.getClass());

    @Value("${splunk.username}")
    private String username;
    @Value("${splunk.password}")
    private String password;
    @Value("${splunk.host}")
    private String host;
    @Value("${splunk.port}")
    private Integer port;
    @Value("${splunk.schema}")
    private String schema;
    @Value("${splunk.app}")
    private String splunkApp;

    @Override
    public Service getService() {
        logger.debug(">>>>> 连接Splunk服务器，地址：" + host);
        ServiceArgs args = new ServiceArgs();
        args.setUsername(username);
        args.setPassword(password);
        args.setHost(host);
        args.setPort(port);
        args.setScheme(schema);
        args.setApp(splunkApp);
        args.setSSLSecurityProtocol(SSLSecurityProtocol.TLSv1_2);
        Service service = Service.connect(args);
        logger.debug("<<<<< 连接Splunk服务器，地址：" + host);
        return service;
    }
}
